package com.global.college.api;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;

import com.global.college.R;


/**
 * Created by fu on 2020/2/18.
 * Describe: 控制缓冲框显示与隐藏
 */
public class ProgressDialogHandler extends Handler {

    public static final int SHOW_DIALOG = 1;

    public static final int DISMISS_DIALOG = 2;


    private boolean mCancelable;

    private ProgressDialogListener mProgressDialogListener;

    private Context mContext;

    private CustomProgress mDialog;

    public ProgressDialogHandler(boolean mCancelable, ProgressDialogListener mProgressDialogListener, Context context) {
        this.mCancelable = mCancelable;
        this.mProgressDialogListener = mProgressDialogListener;
        this.mContext = context;
    }


    /**
     * 用于显示Dialog
     */
    private void initProgressDialog() {
        if (mDialog == null) {
            mDialog = CustomProgress.show(mContext, mContext.getString(R.string.cube_ptr_loading), mCancelable, null);
            mDialog.setCancelable(mCancelable);
            if (mCancelable) {
                mDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialogInterface) {
                        mProgressDialogListener.onCancelProgress();
                    }
                });
            }
        }
        if (!mDialog.isShowing()) {
            mDialog.show();
        }
    }

    private void dismissProgressDialog() {
        try {
            if (null != mContext && mDialog != null && mDialog.isShowing()) {
                mDialog.dismiss();
                mDialog.cancel();
            }
        } catch (Exception e) {
        } finally {
            mDialog = null;
        }
    }

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case SHOW_DIALOG:
                initProgressDialog();
                break;
            case DISMISS_DIALOG:
                dismissProgressDialog();
                break;
            default:
                break;
        }
    }
}
